<?php
class XML
{
	private $xml;
	private $encoding;
	function XML($file, $encoding = null, $isfile = true)
	{
		$this->encoding = ($encoding)?$encoding:Sslib::Variable('page.charset');
		$this->xml = new XMLReader();
		if($isfile)	$this->xml->open($file);
		else 		$this->xml->XML($file);
    	
	}
	function ToAssoc()
	{
        $assoc = NULL;
        $n = 0;
        while($this->xml->read()){
            if($this->xml->nodeType == XMLReader::END_ELEMENT) break;
            if($this->xml->nodeType == XMLReader::ELEMENT){
                $assoc[$n]['name'] = $this->xml->name;
                if($this->xml->hasAttributes) while($this->xml->moveToNextAttribute()) $assoc[$n]['atr'][$this->xml->name] = $this->xml->value;
                $assoc[$n]['val'] = $this->ToAssoc($this->xml);
                $n++;
            }
            else if($this->xml->nodeType == XMLReader::TEXT) $assoc = $this->xml->value;
        }
        return $assoc;
	}
	function __destruct()
	{
		$this->xml->close();
	}
}
?>